<!DOCTYPE html>
<html lang="en">
<head>
    <title>Magic Square!</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://getbootstrap.com/docs/4.0/dist/css/bootstrap.min.css">

    <style>
        #square-size {
            margin-left: 100px;
            margin-top: 100px;
            width: 150px;
        }

        #start-num {
            margin-left: 100px;
            width: 150px;

        }

        /* CSS */
        .button-3 {
            margin-left: 100px;
            margin-top: 10px;
            appearance: none;
            background-color: #2ea44f;
            border: 1px solid rgba(27, 31, 35, .15);
            border-radius: 6px;
            box-shadow: rgba(27, 31, 35, .1) 0 1px 0;
            box-sizing: border-box;
            color: #fff;
            cursor: pointer;
            display: inline-block;
            font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
            font-size: 14px;
            font-weight: 600;
            line-height: 20px;
            padding: 6px 16px;
            position: relative;
            text-align: center;
            text-decoration: none;
            user-select: none;
            -webkit-user-select: none;
            touch-action: manipulation;
            vertical-align: middle;
            white-space: nowrap;
        }

        .button-3:focus:not(:focus-visible):not(.focus-visible) {
            box-shadow: none;
            outline: none;
        }

        .button-3:hover {
            background-color: #2c974b;
        }

        .button-3:focus {
            box-shadow: rgba(46, 164, 79, .4) 0 0 0 3px;
            outline: none;
        }

        .button-3:disabled {
            background-color: #94d3a2;
            border-color: rgba(27, 31, 35, .1);
            color: rgba(255, 255, 255, .8);
            cursor: default;
        }

        .button-3:active {
            background-color: #298e46;
            box-shadow: rgba(20, 70, 32, .2) 0 1px 0 inset;
        }

        th, td {
            padding: 15px;
            text-align: left;
        }

        #magic-square {
            font-family: Arial, Helvetica, sans-serif;
            border-collapse: collapse;
            border: 1px solid black;
            visibility: visible;
            margin-top: 150px;
            margin-left: auto;
            margin-right: auto;
            width: 500px;
        }

        #magic-square td {
            padding-top: 12px;
            padding-bottom: 12px;
            text-align: center;
            color: black;
            border: 1px solid black;
        }

    </style>

    <script src="https://code.jquery.com/jquery-3.7.1.js"></script>

    <script>
        $(function () {
            $("#gen-square").click(function () {
                var dim = parseInt(document.getElementById('square-size').value);
                var errTxt = document.getElementById("error-msg");
                var tbl = document.getElementById("magic-square");
                var startNum = parseInt(document.getElementById("start-num").value);

                if (dim % 2 == 1) {
                    buildTable(dim, tbl);
                    generateSquare(dim, startNum);
                    errTxt.innerText = '';
                } else {
                    errTxt.innerText = 'Dimensions of table must be positive and odd!';
                }
            });

            function buildTable(dim, tbl) {

                if (tbl != null && tbl.rows.length > 0) {
                    for (i = 0; i < tbl.rows.length; i++)
                        tbl.deleteRow(0);

                    tbl.deleteRow(0);
                }


                for (i = 0; i < dim; i++) {
                    var row = tbl.insertRow(i);

                    for (j = 0; j < dim; j++)
                        row.insertCell(j);
                }
            }

            function generateSquare(n, startNum) {
                magicSquare = Array(n).fill(0).map(x => Array(n).fill(0));

                // Initialize position for 1
                var i = parseInt(n / 2);
                var j = n - 1;

                // One by one put all values in magic square
                for (num = 1; num <= n * n;) {
                    if (i == -1 && j == n) // 3rd condition
                    {
                        j = n - 2;
                        i = 0;
                    } else {
                        // 1st condition helper if next number
                        // goes to out of square's right side
                        if (j == n)
                            j = 0;

                        // 1st condition helper if next number is
                        // goes to out of square's upper side
                        if (i < 0)
                            i = n - 1;
                    }

                    // 2nd condition
                    if (magicSquare[i][j] != 0) {
                        j -= 2;
                        i++;
                        continue;
                    } else
                        // set number
                        magicSquare[i][j] = num++;

                    // 1st condition
                    j++;
                    i--;
                }

                // remove the previous tr's if they exist
                var tbl = document.getElementById('magic-square');

                // build the final table
                for (i = 0; i < n; i++) {
                    var row = tbl.getElementsByTagName('tr')[i];
                    for (j = 0; j < n; j++) {
                        var td = row.getElementsByTagName("td")[j];
                        td.innerText = magicSquare[i][j] + startNum;
                    }
                }
            }
        });
    </script>

</head>
<body>
<div>
    <input type="text"
           id="square-size"
           class="form-control"
           placeholder="Dimension"
           required=""><br/>
    <input type="text"
           id="start-num"
           class="form-control"
           placeholder="Starting number"
           required=""><br/>

    <button id="gen-square"
            type="submit"
            class="button-3"
            role="button">Create Square
    </button>
    <br/>

    <p id="magic-p">
    <table id="magic-square">
        <tbody>
        </tbody>
    </table>
    </p>
    <p id="error-msg" style="margin-left:100px;color:red;">

    </p>
</div>
</body>
</html>